//
//  PsdInputVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/26.
//

import UIKit

class PsdInputVC: ZMViewController {

    //MARK:--------------------属性--------------------
    var phone = ""
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var psdText: UITextField!
    
    @IBOutlet weak var loginBtn: UIButton!
        
    @IBOutlet weak var findBtn: UIButton!
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        psdText.becomeFirstResponder()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        psdText.rx.text.orEmpty.map { [unowned self] s -> Bool in
            return s.zm.match(CONFIG_PSD_REGULAR)
        }.subscribe(onNext:{ [unowned self] resp in
            self.loginBtn.isEnabled = resp
            self.loginBtn.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onFind(_ sender: Any) {
        router.push("xm://main/Login/find_psd", context: ["phone" : phone])
    }
    
    @IBAction func onLogin(_ sender: Any) {
        login()
    }
    
    func login() {
        ZMToast.showActivity()
        LoginAPI.psdLogin(["password" : psdText.text!, "phone" : phone]).rxRequest(type: LoginModels.LoginResponse.self).flatMap { resp -> Observable<CommonModels.UserInfo> in
            // 登录成功
            ZMAppShareData.share.loginInfo = resp
            // 拉取用户信息
            return MineAPI.userInfo([:]).rxRequest(type: CommonModels.UserInfo.self)
        }.subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            ZMAppShareData.share.userInfo.accept(resp)
            ZMAppShareData.share.saveUserInfo()
            self.lgoinSuccessGoNext()
        }).disposed(by: disposeBag)
    }
    
    /// 登录成功后去往的页面
    func lgoinSuccessGoNext() {
        if let vc = LoginService.shared.checkIsInfoOkAndNextVC() {
            replace(vc)
        } else {
            // 没问题可以进入app了
            ZMJumpHelper.destroy(FromVC: self, toFirstClass: LoginVC.classForCoder(), isContainSelf: true, isContainFirst: true)
            navigationController?.popViewController(animated: true)
        }
    }
    
    // 重写返回按钮
    override func customBackButton() -> UIButton? {
        let btn = ZMUtil.button(title: "返回", image: UIImage.init(named: "back_18"), font: UIFont.systemFont(ofSize: 18), color: .black)
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        return btn
    }

    //MARK:--------------------delegate--------------------

}
